﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpf树菜单项拖拽功能Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        ObservableCollection<TreeItem> treeItemlist = new ObservableCollection<TreeItem>();
        public MainWindow()
        {
            InitializeComponent();
            initTree();
        }

        TreeItem moveItem = null;

        void mouseDown(object sender, MouseEventArgs e)
        {
            
            var obj = e.OriginalSource as FrameworkElement;
            var data = obj.DataContext as TreeItem;

            moveItem = data;
        }

        void mouseUp(object sender, MouseEventArgs e)
        {
            var isadd = false;

            if (e.OriginalSource is TextBlock) isadd = true;

            var obj = e.OriginalSource as FrameworkElement;
            var data = obj.DataContext as TreeItem;

            if (moveItem.TextName == data.TextName)
                return;

            var oldParChi = moveItem.Parent?.Childern ?? treeItemlist;
            if (oldParChi == null)
                throw new Exception("旧子树空异常");
            oldParChi.Remove(moveItem);

            //判断为兄弟
            if (isadd == false)
            {
                var list = data.Parent?.Childern ?? treeItemlist;
                var borIndex = list.IndexOf(data) + 1;

                moveItem.Parent = data.Parent;
                list.Insert(borIndex, moveItem);
            }

            //新节点
            if (isadd == true)
            {
                moveItem.Parent = data;
                data.Childern.Add(moveItem);
            }

            moveItem = null;
            data.Add = false;
        }


        void mouseEnter(object sender, MouseEventArgs e)
        {
            if (moveItem == null)
                return;

            var obj = e.OriginalSource as FrameworkElement;
            var data = obj.DataContext as TreeItem;


            if (moveItem.TextName == data.TextName)
                return;

            data.Add = true;
        }

        void mouseLeave(object sender, MouseEventArgs e)
        {
            if (moveItem == null)
                return;

            var obj = e.OriginalSource as FrameworkElement;
            var data = obj.DataContext as TreeItem;

            data.Add = false;
        }


        private void initTree()
        {
            var root1 = new TreeItem
            {
                TextName = "root1"
            };
            var root2 = new TreeItem
            {
                TextName = "root2"
            };
            var root3 = new TreeItem
            {
                TextName = "root3",
                Childern = new ObservableCollection<TreeItem> { new TreeItem { TextName = "233" } }
            };

            foreach (var item in root3.Childern)
            {
                item.Parent = root3;
            }
            treeItemlist.Add(root1);
            treeItemlist.Add(root2);
            treeItemlist.Add(root3);
            treeView.ItemsSource = treeItemlist;
        }
    }
}
